/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cdp.cdf.component.metadata.serializer.obsolete.commonCmp;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.caf.cef.schema.base.element.impl.DefaultOperation;
import com.inspur.edp.caf.cef.schema.base.element.serializer.template.OperationSerializerTemplate;
import com.inspur.edp.caf.cef.schema.base.extension.BaseExtensionSerializer;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.cdp.cdf.component.manager.serializer.CompParameterSerializer;
import com.inspur.edp.cdp.cdf.component.manager.serializer.DefaultCompSerializerContext;
import com.inspur.edp.cdp.cdf.component.manager.serializer.JsonFieldConstant;
import com.inspur.edp.cdp.cdf.component.manager.serializer.factory.CompDataTypeSerializerFactory;
import com.inspur.edp.cdp.cdf.component.manager.serializer.factory.CompParameterSerializerFactory;
import com.inspur.edp.cdp.cdf.component.metadata.serializer.CommonCompOperationSerializer;
import com.inspur.edp.component.schema.GspCompOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class CommonCompOpSerializer extends CommonCompOperationSerializer {
    private final DefaultCompSerializerContext context = new DefaultCompSerializerContext();
    private static final Logger logger = LoggerFactory.getLogger(CommonCompOpSerializer.class);

    public CommonCompOpSerializer() {
        this.initSerializerContext2();
    }

    private void initSerializerContext2() {
        //构造SerializeContext
        CompParameterSerializerFactory parameterSerializerFactory = new CompParameterSerializerFactory();
        parameterSerializerFactory.setGspCompParamSerializer(this.getParameterSerializer());
        context.setParameterSerializerFactory(parameterSerializerFactory);
        CompDataTypeSerializerFactory dataTypeSerializerFactory = new CommonCompDataTypeSerializerFactory();
        context.setDataTypeSerializerFactory(dataTypeSerializerFactory);
    }

    //region serialize

    /**
     * 序列化
     */
    @Override
    public JsonNode serialize(GspCompOperation method) {
        JsonNode result = (new ObjectMapper()).createObjectNode();
        writeBaseInfo(method, result);
        writeExtendInfo(method, result);

        return result;
    }

    private void writeBaseInfo(GspCompOperation method, JsonNode result) {
        //序列化id
        JsonUtil.setPropValue(result, JsonFieldConstant.OperationField.ID, method.getId());
        //序列化父类属性
        OperationSerializerTemplate baseSerializer = new OperationSerializerTemplate();
        baseSerializer.serialize(result, method, this.context);
        JsonUtil.setPropValue(result, "kind", DefaultOperation.KIND);
        BaseExtensionSerializer.serializer(method, result);
    }

    @Override
    protected CompParameterSerializer getParameterSerializer() {
        return new CommonCompParamSerializer();
    }
}
